define(function (require, exports, module) {
   var layer = require('layer');
   var ajax = require('mod/ajax');
    (function (doc) {
        var joinForm={
            init:function () {
                this.form=doc.querySelector('#joinForm');
                this.realName=doc.querySelector('#realName');
                this.phoneNumber=doc.querySelector('#phoneNumber');
                this.weChatNum=doc.querySelector('#weChatNum');
                this.city=doc.querySelector('#city');
                this.brandName=doc.querySelector('#brandName');
                this.shopName=doc.querySelector('#shopName');
                this.shopSite=doc.querySelector('#shopSite');
                this.initEvents();
            },
            initEvents:function () {
                this.form.addEventListener('submit',this.valid.bind(this));
            },
            valid:function (e) {
                e.preventDefault();
                var who=this.form.getAttribute("data-who");//0:搭配师 1：设计师 2：商家
                var realName=this.realName.value;
                var phoneNumber=this.phoneNumber.value;
                var weChatNum=this.weChatNum.value;
                var city=this.city.value;
                var brandName="";
                var shopName="";
                var shopSite="";
                var reg=new RegExp("^1\\d{10}$");//手机号正则验证

                if(realName.trim()==""){
                    layer.msg("请填写姓名",{
                        offset:'100px'
                    });
                    return;
                }else if(phoneNumber.trim()==""){
                    layer.msg("请填写手机号",{
                        offset:'100px'
                    });
                    return;
                }else if(!reg.test(phoneNumber.trim())){
                    layer.msg("请正确填写手机号",{
                        offset:'100px'
                    });
                    return;
                } else if(weChatNum.trim()==""){
                    layer.msg("请填写微信号",{
                        offset:'100px'
                    });
                    return;
                }else if(city.trim()==""){
                    layer.msg("请填写所在城市",{
                        offset:'100px'
                    });
                    return;
                }
                if(who=="1"||who=="2"){
                    brandName=this.brandName.value;
                    if(brandName.trim()==""){
                        layer.msg("请填写品牌名称",{
                            offset:'100px'
                        });
                        return;
                    }
                }
                if(who=="2"){
                    shopName=this.shopName.value;
                    if(shopName.trim()==""){
                        layer.msg("请填写店铺名称",{
                            offset:'100px'
                        });
                        return;
                    }
                    shopSite=this.shopSite.value;
                    if(shopSite.trim()==""){
                        layer.msg("请填写店铺地址",{
                            offset:'100px'
                        });
                        return;
                    }
                }

                var data="post="+who+"&realName="+realName+"&phoneNumber="+phoneNumber+"&weChatNum="+weChatNum+"&city="+city+"&brandName="+brandName+"&shopName="+shopName+"&shopSite="+shopSite;
                this.sendSubmit(data);

            },
            sendSubmit:function (data) {
                var self=this;
                new ajax({
                    type:"post",
                    url:"/joinUs",
                    data:data,
                    success:function (xhr) {
                        if(xhr.responseXML.documentElement.querySelector("code").textContent=="1"){
                            layer.msg("入驻成功",{
                                offset:'100px'
                            });
                            setTimeout(function () {
                                self.form.reset();
                            },1000);
                        }else{
                            layer.msg("入驻失败",{
                                offset:'100px'
                            });
                        }
                    }
                });

            }
        }

        window.addEventListener('load',function () {
            joinForm.init();
        })

    })(document);

});
